//
//  TextViewWithRequiredView.swift
//  GreenDeal
//
//  Created by Moennig Q on 1/3/2023.
//

import UIKit
enum TextViewContentType: Int{
    case text = 0//一般的文本而已
    case addtionalInfo//附件信息有字数的限制
    
}
class TextViewWithRequiredView: UIView {
    var endEditingClosure: ((String)->Void)?
    var isValidFlag: Bool = true//当前text的内容是否有效
    var needCheckFlag: Bool = true//true表示需要检验
    private var contentType: TextViewContentType = .text
    lazy var textView: BaseTextView = {
        return ViewFactory.shared.generateTextView()
    }()
    lazy var requiredLabel: BaseLabel = {
        return ViewFactory.shared.generateRequiredLabel()
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpView()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init(_ type: TextViewContentType = .text, needCheckFlag: Bool = true) {
        super.init(frame: CGRect.zero)
        self.contentType = type
        self.needCheckFlag = needCheckFlag
        self.setUpView()
        
    }
    private func setUpView(){
        self.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(130)
        }
        //required label
        self.addSubview(requiredLabel)
        requiredLabel.snp.makeConstraints { make in
            make.top.equalTo(textView.snp.bottom).offset(2)
            make.left.right.bottom.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //监听文本编辑完成在校验信息，控制是否显示提示信息
        textView.delegate = self
    }
    //MARK: - 检查必填项的
    func checkTextViewIfValid(){
        self.hideInvalidMsg()
        //不需要校验时，隐藏
        if self.needCheckFlag == false{
            self.isValidFlag = true
            return
        }
        let content = (textView.text ?? "").trimmingCharacters(in: .whitespaces)
        if content == ""{//一般的文本
            self.showInvalidMsg(R.string.localizable.thisFieldIsRequird())
        }else{
            if self.contentType == .addtionalInfo{
                if Tool().isValidAddtionalInfo(content) == false{
                    self.showInvalidMsg(R.string.localizable.additionalUpgradeSystemInformationMustBeBetween7And4000Digits())
                }
            }
        }
    }
    //MARK: - 隐藏tip  msg
    func hideInvalidMsg(){
        self.textView.layer.borderColor = UIColor(hexStr: "#292D3233").cgColor
        self.requiredLabel.text = ""
        self.isValidFlag = true//校验合法
    }
    //MARK: - 显示msg
    func showInvalidMsg(_ msg: String){
        self.textView.layer.borderColor = UIColor(hexStr: "#ED3E3EFF").cgColor
        self.requiredLabel.text = msg
        self.isValidFlag = false//校验不合法，
    }
}
extension TextViewWithRequiredView: UITextViewDelegate{
    func textViewDidEndEditing(_ textView: UITextView) {
        self.checkTextViewIfValid()
        self.endEditingClosure?(self.textView.text ?? "")
    }
}
